var Weapon = require("Weapon");
var EnemyDamager = require("EnemyDamager");
var UIController = require("UIController");
var Player = require("PlayerController");

cc.Class({
	extends: Weapon,

	properties: {

		gohei: cc.Prefab,


		controlV2: cc.Vec2,
		destinationV2: cc.Vec2,

		liveTime: 0,

	},

	// LIFE-CYCLE CALLBACKS:


	onLoad() {
		this.degree = 0;

	},

	start() {

		this.spawnGoheis();
	},

	update(dt) {

		if (Player.instance.getPlayerDrgree()) {
			this.degree = Player.instance.getPlayerDrgree();
		};

		if (this.statsUpdated == true) {
			this.statsUpdated = false;
 
			this.unscheduleAllCallbacks();
			this.spawnGoheis();
		}
	},

	spawnGoheis(){
		this.schedule(() => {
			this.schedule(() => {

				let goheiBullet = cc.instantiate(this.gohei);

				goheiBullet.parent = cc.find('Canvas');
				goheiBullet.setPosition(this.node.convertToWorldSpaceAR(cc.v2(this.position)).sub(cc.v2(cc.find('Canvas').position)));

				let randomRank = (Math.random() - 0.5) * 2;
				let bulletAnimation;
				if (randomRank > 0) {
					goheiBullet.scaleX = -goheiBullet.scaleX;
					bulletAnimation = goheiBullet.getComponent(cc.Animation).play('GoheiCWSpin');
				} else {

					bulletAnimation = goheiBullet.getComponent(cc.Animation).play('GoheiCCWSpin');
				}
				bulletAnimation.wrapMode = cc.WrapMode.Loop;
				bulletAnimation.repeatCount = Infinity;
				this.setStats(goheiBullet);

				let goheiShot = cc.tween(goheiBullet)
				
					.bezierBy(this.stats[this.weaponLevel].range / this.stats[this.weaponLevel].speed,
						cc.v2(this.position),
						cc.v2(this.position).add(cc.v2(this.controlV2.x * randomRank, this.controlV2.y)),
						cc.v2(this.position).add(cc.v2(this.destinationV2.x * randomRank, this.destinationV2.y)), { easing: 'sineOut' })
					.removeSelf()
					.start();

			}, 0.3, this.stats[this.weaponLevel].amount - 1);

		}, this.stats[this.weaponLevel].duration);
	},

	setStats(goheiBullet) {
		goheiBullet.getComponent('EnemyDamager').isBullet = true;
		goheiBullet.getComponent('EnemyDamager').validHits = 3;
		goheiBullet.getComponent('EnemyDamager').damageAmount = this.stats[this.weaponLevel].damage;
	},
});

